import java.util.Scanner;
import java.lang.Thread;
class elevator{
	
	public String direction="stay";//up or down
	public int curFloor=1;//which floor is elevator in;
	public int targetFloor=-1;
	private boolean isDoorOpen = false;
	private int stay=0;
	person p;

	Thread sp = new Thread();
	floorButton f;
	building b;
	GUI g = new GUI();
	public int[] go = new int[7];

	public elevator(){
		for(int i=0;i<7;i++)go[i] = 0;
	}

	private void setMove(person p)throws Exception{
		int data = p.getTargetFloor();
		go[data] = 1;
		if(data>targetFloor) {
			targetFloor = data;
			go[targetFloor] = 1;
		}
		startMove();
	}

	private void startMove()throws Exception{
		int s;
		if(targetFloor>curFloor) {
			s=targetFloor-curFloor;
			while(s-->0){
				if(go[curFloor] == 1){
					go[curFloor++]=0;
					Thread.sleep(5000);
				}
				else curFloor++;
				Thread.sleep(2000);
				System.out.println(curFloor+"is arriving!");
			}
		}

		else{
			s=curFloor-targetFloor;
			while(s-->0){
				if(go[curFloor] == 1){
					go[curFloor--]=0;
					Thread.sleep(5000);
				}
				else curFloor--;
				Thread.sleep(2000);
				System.out.println(curFloor+"is arriving!");
			}
		}
		targetFloor = -1;
		direction ="stay";
		System.out.println("OK!");
	}

	public int isNeedStay(int index){
		int x=index/10;
		int y=index%10;
		if((direction=="up" && y==2)|| (direction=="down" && y==1) && 
			((curFloor<x && targetFloor>x)||(curFloor>x && targetFloor<x))) return 0;
		if(targetFloor==-1) return -1;
		else return 1;
		
	}

	private void stayAWhile(int t){
		this.isDoorOpen = true;
		System.out.println("please go into the elevator within 4 second");
		try{
			sp.sleep(5000);
			//bthis.setMove();
		}
		catch(Exception x){
			System.out.println("elevator happend some trouble,server is stop!");
			System.exit(1);
		}
		this.isDoorOpen = false;

	}
	public void call(person p)throws Exception{
		this.p = p;
		int data =  p.getcurFloor();
		go[data] = 1;
		if(data>curFloor && data>targetFloor){ targetFloor = data;direction="up";}
		if(data<curFloor && data<targetFloor){ targetFloor = data;direction="down";}
		setMove(p); 
	}
	public void setCall(int index){
		
	}
	public boolean closeDoor(){
		return true;
	}
	public boolean openDoor(){
		return true;
	}
	public boolean isDoorOpen(){
		return isDoorOpen;
	}
	public int getCurFloor(){
		return curFloor;
	}

}

